<?php

namespace App\Admin\Repositories;

use App\Models\WxAttachment as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class WxAttachment extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    /**
     * 把附件保存到附件表
     * @param $form
     * @param $value
     */
    public function saveAttachment($form,$value){
        if ($form->isEditing() && ! $value) {
            // 编辑页面，删除图片逻辑
            Model::destroy($form->model()->image1);

            return;
        }

        // 新增或编辑页面上传图片
        if ($value) {
            $model = Model::where('path', $value)->first();
        }

        if (empty($model)) {
            $model = new Model();
        }

        $model->path = $value;

        $model->save();

        return $model->getKey();

    }



}
